package com.atguigu.rabbitmq.seven;

import com.atguigu.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/*
* 消息消费者
* 这里测试的，目的是：测试topic交换机
* */
public class ReceiveLogsTopic01 {

    /*交换机名字*/
    public static final String EXCHANGE_NAME="topic_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        /*通过connection创建channel*/
        Channel channel = RabbitMqUtils.getChannel();

        /*因为交换机已经声明过了，所以这里不需要声明*/
        /*声明队列*/
        String queueName="Q1";
        channel.queueDeclare(queueName, false, false, false, null);
        /*将交换机和队列绑定起来*/
        channel.queueBind(queueName, EXCHANGE_NAME, "*.orange.*");
        System.out.println("等待接收消息.......");
        /*消息发送成功的回调*/
        DeliverCallback deliverCallback=(consumerTag,delivery)->{
          String message=new String(delivery.getBody(),"UTF-8");
            System.out.println("接收队列："+queueName+",bindingKey:"+delivery.getEnvelope().getRoutingKey()+",消息："+message);

        };
        CancelCallback cancelCallback=consumerTag->{
            System.out.println("接收消息失败~");
        };
        channel.basicConsume(queueName, true, deliverCallback, cancelCallback);
    }

}














